package cc.htdf.gwc.datacompare.repository.impl;

import cc.htdf.gwc.datacompare.domain.DataCompare;
import cc.htdf.gwc.datacompare.domain.PrecipTs;
import cc.htdf.gwc.datacompare.repository.PrecipScoreRepository;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/2.
 */
@Repository
public class PrecipScoreRepositoryImpl implements PrecipScoreRepository{

    private static final String TABLE_NAME_DATA_COMPARE = "GWC_DATA_COMPARE";
    private static final String[] TABLE_COLUMNS_DATA_COMPARE = new String[] {
            "STATIONID", "MODELRUNDATETIME", "VALIDDATETIME",
            "FORECAST_QOP1HR_VALUE", "OBSERVATION_QOP1HR_VALUE", "DIFFERENCE_QOP1HR_VALUE",
            "FORECAST_TEMPERATURE_VALUE", "OBSERVATION_TEMPERATURE_VALUE", "DIFFERENCE_TEMPERATURE_VALUE"
    };
    private static final String TABLE_NAME_DATA_SCORE = "GWC_DATA_SCORE";
    private static final String[] TABLE_COLUMNS_DATA_SCORE = new String[] {
            "MODELRUNDATETIME", "PARAMA","PARAMB","PARAMC","PARAMD",
            "PC", "TS", "MAR","FAR"
    };

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 批量存储比较结果
     *
     * @param datas
     */
    @Override
    public void batchSaveScoreData(List<DataCompare> datas) throws MySQLIntegrityConstraintViolationException, DuplicateKeyException {
        String columns = String.join(",", TABLE_COLUMNS_DATA_COMPARE);
        String sql = String.format("INSERT INTO %s(%S) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", TABLE_NAME_DATA_COMPARE, columns);
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                DataCompare data = datas.get(i);
                ps.setString(1, data.getStation());
                ps.setString(2, data.getModelRunDateTime());
                ps.setString(3, data.getValidDateTime());
                ps.setString(4, data.getForecastQop1hrValue());
                ps.setString(5, data.getObservationQop1hrValue());
                ps.setString(6, data.getDifferenceQop1hrValue());
                ps.setString(7, data.getForecastTemperatureValue());
                ps.setString(8, data.getObservationTemperatureValue());
                ps.setString(9, data.getDifferenceTemperatureValue());
            }

            @Override
            public int getBatchSize() {
                return datas.size();
            }
        });
    }

    /**
     * 查询降雨时段
     */
    @Override
    public List<Map<String, Object>> queryPrecip() {
        String sql = "SELECT datatime FROM DATATIME_SCORE WHERE datatime >= '2017-09-25'";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public void saveScoreData(List<PrecipTs> datas) throws MySQLIntegrityConstraintViolationException, DuplicateKeyException {
        String columns = String.join(",", TABLE_COLUMNS_DATA_SCORE);
        String sql = String.format("INSERT INTO %s(%S) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", TABLE_NAME_DATA_SCORE, columns);
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                PrecipTs data = datas.get(i);
                ps.setString(1, data.getModelRunDateTime());
                ps.setString(2, data.getParamA());
                ps.setString(3, data.getParamB());
                ps.setString(4, data.getParamC());
                ps.setString(5, data.getParamD());
                ps.setString(6, data.getAccuracy());
                ps.setString(7, data.getTs());
                ps.setString(8, data.getLeak());
                ps.setString(9, data.getEmpty());
            }

            @Override
            public int getBatchSize() {
                return datas.size();
            }
        });
    }
}
